<?php
namespace app\common\model;

use think\Db;
use think\Model;
use think\Session;
class LuntanVote extends Model
{
	protected $insert = ['create_time'];

	 /**
     * 自动生成时间
     * @return bool|string
     */
    protected function setCreateTimeAttr()
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * 获得栏目数组,并且进行level
     * @return array
     */
    public function getCategoryList()
    {
        $categorylist = Db('luntan_category')->order(['sort'=>"DESC"])->select();
        return array2level($categorylist);
    }

    /**
     * 获得投票数组
     * @return array
     */
    public function getVoteList($cid,$keyword,$page)
    {
    	$map = [];
    	if (!empty($keyword)) {
            $map['name'] = ['like', "%{$keyword}%"];
        }
        if (!empty($cid)) {
            $map['cid'] = ['=', "{$cid}"];
        }
    	$votelist = $this->where($map)->paginate(15, false, ['page' => $page]);
    	//循环获得此次投票所属的栏目信息,以及发起投票人的信息
    	$i = 0;
    	for(;$i < count($votelist);$i++){
    		//获得栏目名称
    		$categoryname = Db('luntan_category')->where('id',$votelist[$i]['cid'])->column('name');
    		$votelist[$i]['categoryname'] = $categoryname[0];
    		//获得用户名称 需要判断是否是后台的用户
    		if($votelist[$i]['is_user'] == 1){
    			$username = Db('user')->where('id',$votelist[$i]['uid'])->column('username');
    			$votelist[$i]['username'] = $username[0];
    		}else{
    			$username = Db('admin_user')->where('id',$votelist[$i]['uid'])->column('username');
    			$votelist[$i]['username'] = $username[0];
    		}
    		
    		//获得选项
    		$votelist[$i]['selectlist'] = Db('luntan_voteselect')->where('vid',$votelist[$i]['id'])->select();
    	}
    	return $votelist;
    }

    /**
    *处理添加投票是穿过来的数据
    *@return array
    */
    public function getDealList($data)
    {
    	//$data1投票信息
    	$data1 = [
    		'cid'   	  => $data['cid'],
    		'name'  	  => $data['name'],
    		'status'      => $data['status'],
    		'type'        => $data['type'],
    		'create_time' => $data['create_time'],
    	];
    	if ($data['is_user'] == 2) {
    		$data1['uid'] 	  = Session::get('admin_id');
    		$data1['is_user'] = 2;
    	}
    	//将$data1存入数据库,并且取得存入的id
    	$result1 = $this->save($data1);	
    	if($result1){
    		$vid = $this->id;
    		//获得投票选项信息
	    	$selectdata = $data['select'];
	    	$i = 0;
	    	for(;$i<count($selectdata);$i++){
	    		$arr1[] = [
	    			'vid'  => $vid,
	    			'name' => $selectdata[$i],
	    			'create_time' => $data['create_time']
	    		];
	    	}
	    	return  $arr1;
    	}else{
    		return false;
    	}
    	
    }


    /**
    *更新信息 编辑信息页面所需要的数据
    *@return array
    */
    public function getVoteInfo($id)
    {
    	$voteinfo   = $this->find($id);
    	$voteinfo['select'] = Db('luntan_voteselect')->where('vid',$id)->select();
    	return $voteinfo;
    }

    /**
    *更新信息 更新数据库中的信息
    *@return int
    */
    public function updateDatabase($data)
    {
    	//更新luntan_vote表
    	$data1 = [
    		'cid'   	  => $data['cid'],
    		'name'  	  => $data['name'],
    		'status'      => $data['status'],
    		'type'        => $data['type'],
    		'create_time' => $data['create_time'],
    	];
    	$result1 = $this->save($data1,['id'=>$data['id']]);
    	//更新luntan_voteselect表
    	$list  = $data['ids'];
    	$list1 = $data['select'];
    	$i = 0;
    	for(;$i < count($list);$i++){
    		//判断是否和数据库中的相同
    		$selectname = Db('luntan_voteselect')->where('id',$list[$i])->column('name');
    		if ($selectname[0] == $list1[$i]) {
    			$result2[] = 1;
    		}else{
    			$result2[] = Db('luntan_voteselect')->where('id',$list[$i])->update(['name'=>$list1[$i]]);
    		}    		
    	}
    	//判断成功与否
    	if($result1 && $result2){
    		return 1;
    	}else{
    		return 0;
    	}

    }
    
}